package com.kasa.zto;


/**
 * <p>Title: 解码器</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author gary
 * @version 1.0
 */
public class SOSDecoder extends ZTODecoder
{
    private byte[] data;

    private int pos = 0;

    public SOSDecoder(byte[] data)
    {
        this.data = data;
    }
    
    public int getPos()
    {
        return this.pos;
    }
    
    /**
     * 父类的方式，速度较慢，故重写
     */
    @Override
    protected byte getByte()
    {
        return data[pos++];
    }
    
    protected byte[] getBytes(int size)
    {
    	byte[] bytes = new byte[size];
    	System.arraycopy(data, pos, bytes, 0, size);
    	pos += size;
    	return bytes;
    }
    
    /**
     * 以当前pos位置，解码digit个字节为数字
     * 对应的编码方法是Encoder.encodeNumber(byte digit)
     * 1 byte -128~127
     * 2 byte -32768~32767
     * 3 byte -8388608~8388607
     * 4 byte -2147483648~2147483647
     * 5 byte -549755813888~549755813887
     * 6 byte -140737488355328~140737488355327
     * 7 byte -36028797018963968~36028797018963967
     * 8 byte -9223372036854775808~9223372036854775807
     */    
    public long decode_long(byte digit) throws ZTOException
    {
    	byte abs_digit = (byte)Math.abs(digit); 
        long l = 0;
        for (int i = 0; i < abs_digit; i++) {
            l = l | (data[pos++] & 0xFFL) << (8 * (abs_digit - i - 1));
        }
        return digit < 0 ? -l : l;
    }
}
